import 'package:any_reader/model/article.dart';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ReaderView extends StatelessWidget {
  final Article article;
  final int page;
  final double fontSize;
  final double lineHeight;
  final Color textColor;
  final double padding;
  final double readeAreaHeight;
  final String chapterTitle;
  final Decoration decoration;
  ReaderView(
      {this.article,
      this.page,
      this.fontSize,
      this.lineHeight,
      this.textColor,
      this.padding,
      this.chapterTitle,
      this.readeAreaHeight,
      this.decoration});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: decoration,
      child: Stack(
        children: <Widget>[
          buildContent(article, page),
          Positioned(
            bottom: 0,
            left: 0,
            height: ScreenUtil().setHeight(100),
            width: ScreenUtil().setWidth(750),
            child: Container(
              alignment: Alignment.centerLeft,
              child: Text(
                "    " + chapterTitle,
                style: TextStyle(
                    fontSize: fontSize - ScreenUtil().setSp(fontSize - 10),
                    letterSpacing: 1,
                    wordSpacing: 0,
                    height: lineHeight,
                    color: Color(0xFF666666)),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget buildContent(Article article, int page) {
    return Container(
      child: () {
//        return Container(
//          child: Text("2333| $page | ${article.pageOffsets.length}"),
//        );
        PagePainter _pagePainter;
        _pagePainter = PagePainter(item: article.pageOffsets[page]['page']);
        return CustomPaint(
          painter: _pagePainter,
        );
      }(),
    );
  }
}

class PagePainter extends CustomPainter {
  List<List<Map<String, dynamic>>> item;
  PagePainter({
    this.item,
  });

  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    for (int j = 0; j < item.length; j++) {
      for (int k = 0; k < item[j].length; k++) {
        Map<String, dynamic> _item = item[j][k];
        canvas.drawParagraph(_item["paragraph"], _item["offset"]);
      }
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
    throw UnimplementedError();
  }
}

//    List<PagePainter> pages;
//    for (int i = 0; i < article.pageOffsets.length; i++) {
//      PagePainter _pagePainter;
//      for (int j = 0; j < article.pageOffsets[i].length; j++) {
//        for (int k = 0; k < article.pageOffsets[i][j].length; k++) {
//          Map<String, dynamic> item = article.pageOffsets[i][j][k];
//          _pagePainter = PagePainter(item: item);
//        }
//      }
//      pages.add(_pagePainter);
//    }
//    var content = article.stringAtPageIndex(page);
//    if (content.startsWith('\n')) {
//      content = content.substring(1);
//    }
//    return Container(
//      height: readeAreaHeight,
//      margin: EdgeInsets.fromLTRB(0, ScreenUtil().setHeight(10), 0, 0),
//      padding: EdgeInsets.fromLTRB(padding, 0, padding, 0),
//      child: Center(
//        child: Container(
//          height: readeAreaHeight,
//          child: Text.rich(
//              TextSpan(
//                style: TextStyle(
//                    fontSize: fontSize * 0.98,
//                    color: textColor,
//                    height: lineHeight * 0.98,
//                    letterSpacing: 0,
//                    wordSpacing: 0,
//                    fontFamily: "dengxian"),
//                text: content,
//              ),
//              locale: Locale('zh', 'CH'),
//              textDirection: TextDirection.ltr,
//              textAlign: TextAlign.justify,
//              softWrap: true),
//        ),
//      ),
//    );
